#include "Settings.h"
#include <QDebug>

QSettings Settings::settings("spbau", "downloader");
const QString Settings::maxDownloadIdKey = "maxDownloadId";
const QString Settings::downloadsKey = "downloads";
const QString Settings::idKey = "id";
const QString Settings::nameKey = "name";
const QString Settings::savingPathKey = "savingPath";
const QString Settings::downloadLinkKey = "downloadLink";
const QString Settings::progressKey = "progress";
const QString Settings::priorityKey = "priority";
const QString Settings::finishedKey = "finished";
const QString Settings::startedKey = "started";
const QString Settings::askBeforeExitKey = "askBeforeExit";
const QString Settings::askBeforeOverwriteKey = "askBeforeOverwrite";
const QString Settings::askBeforeStopDownloadKey = "askBeforeStopDownload";
const QString Settings::askBeforeRemoveDownloadKey = "askBeforeRemoveDownload";
const QString Settings::maxParallelDownloadsKey = "maxParallelDownloads";



int Settings::getMaxDownloadId()
{
	if (!settings.contains(maxDownloadIdKey)) {
		return 0;
	} else {
		return settings.value(maxDownloadIdKey).toInt();
	}
}

void Settings::saveMaxDownloadId(int id)
{
	settings.setValue(maxDownloadIdKey, id);
}

QList<Download*> Settings::getDownloadsInfo()
{
	QList<Download*> result;

	int downloadsNumber = settings.beginReadArray(downloadsKey);
	for (int i = 0; i < downloadsNumber; i++) {
		settings.setArrayIndex(i);

		int id = settings.value(idKey).toInt();
		QString name = settings.value(nameKey).toString();
		QString savingPath = settings.value(savingPathKey).toString();
		QString downloadLink = settings.value(downloadLinkKey).toString();
		int progress = settings.value(progressKey).toInt();
		int priority = settings.value(priorityKey).toInt();
		bool finished = settings.value(finishedKey).toBool();
		bool started = settings.value(startedKey).toBool();

		if (!finished) {
			started = false;
			progress = 0;
		}

		result.append(new Download(name, savingPath, downloadLink,
									   priority, id, finished, started, progress));
	}
	settings.endArray();

	return result;
}

void Settings::saveDownloadsInfo(QList<Download*> downloadsInfo)
{
	settings.beginWriteArray(downloadsKey);
	for (int i = 0; i < downloadsInfo.size(); i++) {
		settings.setArrayIndex(i);
		Download *info = downloadsInfo[i];

		settings.setValue(idKey, info->getId());
		settings.setValue(nameKey, info->getName());
		settings.setValue(savingPathKey, info->getSavingPath());
		settings.setValue(downloadLinkKey, info->getDownloadLink());
		settings.setValue(progressKey, info->getProgress());
		settings.setValue(priorityKey, info->getPriority());
		settings.setValue(finishedKey, info->isFinished());
		settings.setValue(startedKey, info->isStarted());
	}
	settings.endArray();
}

bool Settings::getAskBeforeExit()
{
	if (settings.contains(askBeforeExitKey)) {
		return settings.value(askBeforeExitKey).toBool();
	} else {
		return true;
	}
}

void Settings::saveAskBeforeExit(bool askBeforeExit)
{
	settings.setValue(askBeforeExitKey, askBeforeExit);
}

bool Settings::getAskBeforeStopDownload()
{
	if (settings.contains(askBeforeStopDownloadKey)) {
		return settings.value(askBeforeStopDownloadKey).toBool();
	} else {
		return true;
	}
}

void Settings::saveAskBeforeStopDownload(bool askBeforeStopDownload)
{
	settings.setValue(askBeforeStopDownloadKey, askBeforeStopDownload);
}

bool Settings::getAskBeforeRemoveDownload()
{
	if (settings.contains(askBeforeRemoveDownloadKey)) {
		return settings.value(askBeforeRemoveDownloadKey).toBool();
	} else {
		return true;
	}
}

void Settings::saveAskBeforeRemoveDownload(bool askBeforeRemoveDownload)
{
	settings.setValue(askBeforeRemoveDownloadKey, askBeforeRemoveDownload);
}

bool Settings::getAskBeforeOverwrite()
{
	if (settings.contains(askBeforeOverwriteKey)) {
		return settings.value(askBeforeOverwriteKey).toBool();
	} else {
		return true;
	}
}

void Settings::saveAskBeforeOverwrite(bool askBeforeOverwrite)
{
	settings.setValue(askBeforeOverwriteKey, askBeforeOverwrite);
}

int Settings::getMaxParallelDownloadsNumber()
{
	if (settings.contains(maxParallelDownloadsKey)) {
		return settings.value(maxParallelDownloadsKey).toInt();
	} else {
		return 3;
	}
}

void Settings::saveMaxParallelDownloadsNumber(int maxParallelDownloads)
{
	settings.setValue(maxParallelDownloadsKey, maxParallelDownloads);
}
